package com.juzhebao.buyer.mvp.views.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.juzhebao.buyer.utils.Constants;

import java.io.Serializable;

/**
 * 作者：admin on 2016/12/9 09:52
 */

public abstract class BaseFragment extends Fragment implements View.OnClickListener{
    //序列号接口
    public Serializable serializable;
    //view 视图
    public View view;
    public Activity activity;

    public BaseFragment(Activity activity) {
        this.activity = activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //获取到从activity中传递过来的数据,如果想要拿去其中的数据需要进行强制转换为相应的封装类型
        Bundle bundle = getArguments();
        if (bundle != null) {
            serializable = bundle.getSerializable(Constants.ARGUMENT);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        System.out.println("oncreateview");
        view = inflater.inflate(getLayout(), null);
        initView();
        initListener();
        initData();
        return view;
    }

    /**
     * 获取布局
     *
     * @return int类型的布局
     */
    protected abstract int getLayout();

    /**
     * 设置监听事件
     */
    protected abstract void initListener();

    /**
     * 初始化视图
     */
    public abstract void initView();

    /**
     * 初始化fragment
     *
     * @param serializable 传递过来解析的数据
     *  @param  baseFragment 子类的对象
     * @return 为子类的fragment
     */
    public static BaseFragment onInstance(Serializable serializable,BaseFragment baseFragment) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(Constants.ARGUMENT, serializable);
        baseFragment.setArguments(bundle);
        return baseFragment;
    }

    /**
     * 初始化数据
     */
    public abstract void initData();

    /**
     * 实现点击事件
     */
    @Override
    public abstract void onClick(View view) ;
}
